﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Reflection;

namespace docmgr.WinForm
{
    class BarCodeHook
    {
        public delegate void BarCodeDelegate(BarCodes barCode);
        public event BarCodeDelegate BarCodeEvent;

        public struct BarCodes
        {
            public int VirtKey;//虚拟码
            public int ScanCode;//
            public String KeyName;
            public uint AscII;
            public char Chr;

            public string BarCode;
            public bool IsValid;
            public DateTime Time;
        }

        public struct EventMsg
        {
            public int message;
            public int paramL;
            public int paramH;
            public int Time;
            public int hwnd;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);


        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern bool UnhookWindowsHookEx(int idHook);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern int CallNextHookEx(int idHook,int nCode,Int32 wParam,IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern int GetKeyNameText(int lParam, StringBuilder lpBuffer, int nSize);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern int GetKeyboardState(byte[] pbKeyState);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern bool ToAscii(int VirtualKey,int ScanCode,byte[] lpKeyState,ref uint lpChar,int uFlags);

        delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);

        BarCodes barCode = new BarCodes();
        int hKeyboardHook = 0;

        List<char> _barcode = new List<char>(100);
        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam )
        {
            if(nCode==0)
            {
                EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg));

                if(wParam==0x100) //WM_KEYDOWN=0x100
                {
                    barCode.VirtKey = msg.message & 0xff;//
                    barCode.ScanCode = msg.paramL & 0xff;//

                    StringBuilder strKeyName = new StringBuilder(255);

                    if(GetKeyNameText(barCode.ScanCode*65536,strKeyName,255)>0)
                    {
                        barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '\0' });
                    }
                    else
                    {
                        barCode.KeyName = "";
                    }
                    byte[] kbArray = new byte[256];
                    uint uKey = 0;
                    GetKeyboardState(kbArray);

                    if(ToAscii(barCode.VirtKey,barCode.ScanCode,kbArray,ref uKey,0))
                    {
                        barCode.AscII = uKey;
                        barCode.Chr = Convert.ToChar(uKey);
                    }
                    if(DateTime.Now.Subtract(barCode.Time).TotalMilliseconds>100)
                    {
                        _barcode.Clear();
                    }
                    else
                    {
                        if((msg.message&0xff)==13&_barcode.Count>0)
                        {
                            barCode.BarCode = new String(_barcode.ToArray());
                            barCode.IsValid = true;
                        }
                        _barcode.Add(Convert.ToChar(msg.message & 0xff));
                    }
                    barCode.Time = DateTime.Now;
                    if (BarCodeEvent != null) BarCodeEvent(barCode);
                    barCode.IsValid = false;
                }
            }

            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }
        private static HookProc hookproc;

        public bool Start()
        {
            if(hKeyboardHook==0)
            {
                hookproc = new HookProc(KeyboardHookProc);
                hKeyboardHook = SetWindowsHookEx(13, hookproc, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
            }
            return (hKeyboardHook != 0);
        }

        public bool Stop()
        {
            if(hKeyboardHook!=0)
            {
                return UnhookWindowsHookEx(hKeyboardHook);
            }
            return false;
        }
    }
}   
